package org.pearl.springboot.minio.controller;

import org.pearl.springboot.minio.pojo.query.User;
import org.pearl.springboot.minio.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by TD on 2021/8/26
 */
@RestController
@RequestMapping("/v1")
public class UserController {


    @Autowired
    User user;

    @GetMapping("/test")
    // @CrossOrigin(origins = {"*"})
    @ResponseBody
    public User test(HttpServletResponse response, HttpServletRequest request) throws IOException {
        return user;
    }


    @Autowired
    UserService userService;


    @GetMapping("/user/login")
    public Object login(HttpSession httpSession) {
        User user = new User();
        user.setName("吃个桃桃");
        user.setAge(15);
        user.setPermissionCode("add:user");
        httpSession.setAttribute("user", user);
        return user;
    }

    @RequestMapping(value = {"getUser"}, method = {RequestMethod.GET})
    public Object getUser(HttpServletRequest request) {
        request.setAttribute("test", "hahaha");
        return userService.getUser();
    }


    @GetMapping("/user/{id}")
    public Object getUserById(@PathVariable Long id) {
        User user = new User();
        user.setName("吃个桃桃");
        user.setAge(15);
        return user;
    }

    @PostMapping("/user}")
    public Object saveUser(User user) {
        System.out.println("添加用户");
        return user;
    }

    @DeleteMapping("/user/{id}")
    public Object removeUserById(@PathVariable Long id) {
        return "删除用户" + id;
    }

    @PutMapping("/user")
    public Object updateUser(@RequestParam User user) {
        return "更新用户" + user;
    }


}
